<?php declare(strict_types=1);


namespace PinWuYun\domain\data\YouZanData;



/**
 * Created: by zhiJie
 * Date: 2021/6/30
 */
class OrderData
{
    public function buildOrderInfo(array $params): array
    {
        /** @var array $order (索引数组)*/
        $orders = $params['msg']['full_order_info']['orders'];
        $skuList = [];
        foreach ($orders as $orderItem) {
            $skuList[] = [
                'code' => $orderItem['outer_sku_id'],
                'quantity' => $orderItem['num'],
            ];
        }

        $msg = $params['msg'];
        $full_order_info = $msg['full_order_info'];

        // 收货地址信息
        $order =  [
            // 第三方订单号 <= 32位
            'outOrderNo' => $full_order_info['order_info']['tid'],
            // 区域ID TODO
            'regionId' => $params['regionId'],
            // 收货人详细地址
            'receiverAddr' => $full_order_info['address_info']['delivery_address'],
            // 收货人
            'receiver' => $full_order_info['address_info']['receiver_name'],
            // 收货人电话
//            'receiverPhone' => $params['receiverPhone'],
            // 收货人手机号
            'receiverMobile' => $full_order_info['address_info']['receiver_tel'],
            // 非必填 不填为 0.00 运费，最长十位 整数8位小数2位
            'freight' => $full_order_info['pay_info']['post_fee'],
            // 买家备注
            'buyerRemark' => $full_order_info['remark_info']['buyer_message'],
            // 卖家备注
            'sellerRemark' => $full_order_info['order_info']['remark_info']['trade_memo'],
            // 商品列表 json 串，格式参考下面json
            /**
             *  {
            "code": "SL-ECP-6072",  //必填       商品编码
            "quantity": "1"         //必填       数量
            }
             */
            'skuList' => json_encode($skuList),
        ];
        $holdData = [
            'outOrderNo' => $order['outOrderNo'] ?? '',
            // 商品及库存信息json, 参考下面的demo
            'codeInvList' => $order['skuList'],
            // (限售商品必填)区域ID
            'regionId' => $order['regionId'] ?? '',
        ];
        return ['orderInfo' => $order, 'holdData' => $holdData];
    }


    /**
     * 填充确认发货数据
     * @param $you_tid
     * @param $deliveryNo
     * @return array
     */
    public function buildConfirmSeenData($you_tid, $deliveryNo): array
    {
        return [
            "tid" => $you_tid,
            "out_sid" => $deliveryNo,
            "is_no_express" => 0,
        ];
    }

    /**
     * 填充确认收货数据
     * @param $youTid
     * @param $openId
     * @return array
     */
    public function buildTradeFinish($youTid, $openId): array
    {
        return [
            'tid' => $youTid,
            'yz_open_id' => $openId,
        ];
    }
}